<?php
class Check_csv_m extends CI_Model {
	
        public function __construct() {
            parent::__construct();
            $this->load->database();
        }

        //Read CSV at function readcsv
	public function _getIndex($header, $type) {
		$re1 = '/' . $type . '\(/';
		$re2 = '/\)/';
		$str1 = preg_replace($re1, '', $header);
		$index = preg_replace($re2, '', $str1);
		return $index;
	}
	
	public function _setMessageError($str){
   
		$msgError  = $str.":エラー!";
		return $msgError;		
	}
	public function _checkString($output = null) {
		for ($i = 0; $i < count($output); $i++) {
			if (preg_match('/^\/\*\S|\s*\*\/$/', $output[$i]) || strcmp($output[$i], '') == 0 || strpos($output[$i],"#")!== false){
				// echo "$output[$i]";
				unset($output[$i]);
			}
		}
		return $output;
	}
	public function _checkComment($str,$commentFlags){
		if(strpos($str[0], "/*")!==false){
				$commentFlags = 1;
				// echo "$str[0]<br/>";
		}
		for ($i = 0; $i < count($str); $i++) {
			if(strpos($str[$i], "*/")!==false&& $commentFlags==1){
				$commentFlags = 2;
				// echo "$str[$i]<br/>";
			}
		}
		return $commentFlags;
		 
	}
	public function _parseCSVToLine($csvFile) {
		ini_set('auto_detect_line_endings', true);
		ini_set('memory_limit', '-1');
		ini_set('max_execution_time', '3600');
		$file_handle = fopen($csvFile, 'r') or die("Can't open the file");
		$commentFlags = 0;
		$str = "";
		while (!feof($file_handle)) {
			$str = fgetcsv($file_handle, 1024, ",");
			if(strpos($str[0], "#")!==false){
				continue;	
			}
			$commentFlags = $this->_checkComment($str,$commentFlags);
			if($commentFlags==2){
				$commentFlags = 0;
				continue;
			}
			if($commentFlags==0){
				$line_of_text[] = $str;	
			}
		}
		fclose($file_handle) or die("ファイルが開けれない");
		return $line_of_text;
	}

	public function _readcsv($id_link) {
		$csvFile = $id_link;
		$return_array = array();
		if (mb_check_encoding(file_get_contents($csvFile), 'UTF-8')) {
			$outputs = $this -> _parseCSVToLine($csvFile);
			$CSV = array();
			$CSV_ERROR = array();
			$CSV['Data'] = array();
			$CSV['Testees'] = array();
			$testee_number = 0;
			$q_number = 0;
			
			//TODO dumy CSV
			foreach ($outputs as $output) {
				$output = $this -> _checkString($output);
				if (isset($output[0])) {
					$header = $output[0];
					if (strcmp($header, '﻿TestTitle') == 0) {
						if(isset($output[1])){
							$CSV['TestTitle'] = $output[1];
						}
						else
						{
							$str_error = $this->_setMessageError($header);
							$CSV_ERROR[] = $str_error;
						}
					}
					elseif (strcmp($header, 'TestSubTitle') == 0) {
						if(isset($output[1])){
							$CSV['TestSubTitle'] = $output[1];
						}
						else
						{
							$str_error = $this->_setMessageError($header);
							$CSV_ERROR[] = $str_error;
						}
					}
					elseif (strcmp($header, 'StartDateTime') == 0) {
						$CSV['StartDateTime'] = array();
						if(isset($output[1]) && ctype_digit(strval($output[1]))){
							$CSV['StartDateTime']['YYYY'] = $output[1];
						}
						else
						{
							$str_error = $this->_setMessageError("始まる時間:YYYY");
							$CSV_ERROR[] = $str_error;
						}
						if(isset($output[2]) && ctype_digit(strval($output[2]))){
							$CSV['StartDateTime']['MM'] = $output[2];
						}
						else
						{
							$str_error = $this->_setMessageError("始まる時間:MM");
							$CSV_ERROR[] = $str_error;
						}
						if(isset($output[3]) && ctype_digit(strval($output[3]))){
							$CSV['StartDateTime']['DD'] = $output[3];
						}
						else
						{
							$str_error = $this->_setMessageError("はまる時間:DD");
							$CSV_ERROR[] = $str_error;
						}
						if(isset($output[4])&&ctype_digit(strval($output[4]))&& $output[4]>=0 && $output[4]<=23){
							$CSV['StartDateTime']['h'] = $output[4];
						}
						else
						{
							$str_error = $this->_setMessageError("始まる時間:h");
							$CSV_ERROR[] = $str_error;
						}
						if(isset($output[5])&&ctype_digit(strval($output[5]))&& $output[5]>=0 && $output[5]<=59){
							$CSV['StartDateTime']['m'] = $output[5];
						}
						else
						{
							$str_error = $this->_setMessageError("始まる時間:m");
							$CSV_ERROR[] = $str_error;
						}
						if(isset($output[6]) && ctype_digit(strval($output[6]))&& $output[6]>=0 && $output[6]<=59){
							$CSV['StartDateTime']['s'] = $output[6];
						}
						else
						{
							$str_error = $this->_setMessageError("始まる時間:s");
							$CSV_ERROR[] = $str_error;
						}					
					}
					elseif (strcmp($header, 'EndDateTime') == 0) {
						$CSV['EndDateTime'] = array();
						if(isset($output[1])&&ctype_digit(strval($output[1]))){
							$CSV['EndDateTime']['YYYY'] = $output[1];
						}
						else
						{
							$str_error = $this->_setMessageError("終わる時間:YYYY");
							$CSV_ERROR[] = $str_error;
						}
						if(isset($output[2])&&ctype_digit(strval($output[2]))){
							$CSV['EndDateTime']['MM'] = $output[2];
						}
						else
						{
							$str_error = $this->_setMessageError("終わる時間:MM");
							$CSV_ERROR[] = $str_error;
						}
						if(isset($output[3]) && ctype_digit(strval($output[3]))){
							$CSV['EndDateTime']['DD'] = $output[3];
						}
						else
						{
							$str_error = $this->_setMessageError("終わる時間:DD");
							$CSV_ERROR[] = $str_error;
						}
						if(isset($output[4])&&ctype_digit(strval($output[4]))&& $output[4]>=0 && $output[4]<=23){
							$CSV['EndDateTime']['h'] = $output[4];
						}
						else
						{
							$str_error = $this->_setMessageError("終わる時間:h");
							$CSV_ERROR[] = $str_error;
						}
						if(isset($output[5])&&ctype_digit(strval($output[5]))&& $output[5]>=0 && $output[5]<=59){
							$CSV['EndDateTime']['m'] = $output[5];
						}
						else
						{
							$str_error = $this->_setMessageError("終わる時間:m");
							$CSV_ERROR[] = $str_error;
						}
						if(isset($output[6])&&ctype_digit(strval($output[6]))&& $output[6]>=0 && $output[6]<=59){
							$CSV['EndDateTime']['s'] = $output[6];
						}
						else
						{
							$str_error = $this->_setMessageError("終わる時間:s");
							$CSV_ERROR[] = $str_error;
						}	
					}
					elseif (strcmp($header, 'TestTime') == 0) {
						$CSV['TestTime'] = array();
						if(isset($output[1])&&ctype_digit(strval($output[1]))&& $output[1]>0){
							$CSV['TestTime']['number'] = $output[1];
						}
						else
						{
							$str_error = $this->_setMessageError("テストの何分");
							$CSV_ERROR[] = $str_error;
						}
						if(isset($output[2])&&($output[2]=='h'||$output[2]=='m'||$output[2]=='s')){
							$CSV['TestTime']['unit'] = $output[2];
						}
						else
						{
							$str_error = $this->_setMessageError("テストの時間の単位:分");
							$CSV_ERROR[] = $str_error;
						}						
					}
					elseif (strcmp($header, 'TestType') == 0) {
						if(isset($output[1])&&($output[1]=='Fix'||$output[1]=='Unfix')){
							$CSV['TestType'] = $output[1];
						}
						else{
							$str_error = $this->_setMessageError("テストタイプ");
							$CSV_ERROR[] = $str_error;
						}
					}
					elseif (strcmp($header, 'Testees') == 0) {
						$testee_number++;
						$CSV['Testees'][$testee_number]  = array();
						if(isset($output[1])&&isset($output[2])&& $output[1]=="Name:"){
							$CSV['Testees'][$testee_number]['Name:'] = $output[2];
						}
						else{
							$str_error = $this->_setMessageError("回答者".":".$testee_number.":名前");
							$CSV_ERROR[] = $str_error;
						}
						if(isset($output[3])&&isset($output[4])&& $output[3]=="ID:"){
							$CSV['Testees'][$testee_number]['ID'] = $output[4];
						}
						else{
							$str_error = $this->_setMessageError("回答者".":".$testee_number.":ユーザID");
							$CSV_ERROR[]  = $str_error;
						}
						if(isset($output[5])&&isset($output[6])&& $output[5]=="PW:"){
							$CSV['Testees'][$testee_number]['PW'] = $output[6];
						}
						else{
							$str_error = $this->_setMessageError("回答者".":".$testee_number.":パスワード");
							$CSV_ERROR[] = $str_error;
						}
					}
					elseif (preg_match('/^Q\([0-9]+\)$/', $header)) {
						$index = $this -> _getIndex($header, 'Q');
						// echo "$index.<br/>";
						if ($index == $q_number && !isset($CSV['Data'][$index]['ErrorQuestion'])) {
							if(isset($output[1]))
							{
								if (preg_match('/^S\([0-9]+\)$/', $output[1])) {
									$indexObjSX = $this->_getIndex($output[1],'S');
									if(isset($output[2])&& $indexObjSX==$x){
										if($CSV['Data'][$index]['QSType']=='FG'){
											if ($output[2] == 'FG') {
												if(isset($output[3])){	
												$CSV['Data'][$index]['objSX'][$x] = $output[3];
												}
												else{
													$str_error = $this->_setMessageError($header."-S(".$x.")");
													$CSV_ERROR[] = $str_error;		
												}		
											} else {
												$str_error = $this->_setMessageError($header."-S(".$x.")");
												$CSV_ERROR[] = $str_error;		
											}
											
										}
										elseif($CSV['Data'][$index]['QSType']=='QS'){
											$CSV['Data'][$index]['objSX'][$x] = $output[2];
										}
										else{
											$str_error = $this->_setMessageError($header."-S(".$x.")");
											$CSV_ERROR[] = $str_error;	
										}
									}
									else{
										$str_error = $this->_setMessageError($header."-S(".$x.")");
										$CSV_ERROR[] = $str_error;	
									}
									$x++;
								}
								elseif (preg_match('/^WR\([0-9]+\)$/', $output[1])) {
									$indexObjWR = $this->_getIndex($output[1],'WR');
									if(isset($output[2])&& ctype_digit(strval($output[2])) && $indexObjWR == $y && $CSV['Data'][$index]['QSType']=='QW'){
										$CSV['Data'][$index]['objWR'][$y] = $output[2];
									}
									else{
										$str_error = $this->_setMessageError($header."-WR(".$y.")");
										$CSV_ERROR[] = $str_error;	
									}
									$y++;
								}
								elseif (strcmp($output[1], 'TM') == 0) {
									if(isset($output[2])&& ctype_digit(strval($output[2]))){
										$CSV['Data'][$index]['TMNumber'] = $output[2];
									}
									else{
										$str_error = $this->_setMessageError($header."-TM:何秒");
										$CSV_ERROR[] = $str_error;	
									}
									if(isset($output[3])&&($output[3]=='h'||$output[3]=='m'||$output[3]=='s')){
										$CSV['Data'][$index]['TMUnit'] = $output[3];
									}
									else{
										$str_error = $this->_setMessageError($header."-TM:単位:秒");
										$CSV_ERROR[] = $str_error;	
									}
								}
								elseif (strcmp($output[1], 'LM') == 0) {
									if(isset($output[2]))
									{
										$CSV['Data'][$index]['LMType'] = $output[2];
										if (strcmp($output[2], 'TRI') == 0) {
											if(isset($output[3])&&ctype_digit(strval($output[3]))&&isset($output[4])&&($output[4]=='h'||$output[4]=='m'||$output[4]=='s')){
												$CSV['Data'][$index]['LMData'] = array('number' => $output[3], 'unit' => $output[4]);
											}
											else{
											$str_error = $this->_setMessageError($header."-採点方法TRI:LMData");
											$CSV_ERROR[] = $str_error;		
											}
										}
										elseif (strcmp($output[2], 'REC') == 0) {
											if(isset($output[3])&& ctype_digit(strval($output[3])) &&isset($output[4])&&($output[4]=='h'||$output[4]=='m'||$output[4]=='s')){
												$CSV['Data'][$index]['LMData'] = array('number' => $output[3], 'unit' => $output[4]);
											}
											else{
											$str_error = $this->_setMessageError($header."-採点方法REC:LMData");
											$CSV_ERROR[] = $str_error;		
											}
										}
										elseif (strcmp($output[2], 'TRAP') == 0) {
											if(isset($output[3]) && ctype_digit(strval($output[3])) &&isset($output[4])&&isset($output[5])&&isset($output[6]) &&
											($output[4]=='h'||$output[4]=='m'||$output[4]=='s'||
											$output[6]=='h'||$output[6]=='m'||$output[6]=='s')){
												$CSV['Data'][$index]['LMData'] = array('number1' => $output[3], 'unit1' => $output[4], 'number2' => $output[5], 'unit2' => $output[6]);
											}
											else{
											$str_error = $str_error = $this->_setMessageError($header."-採点方法TRAP:LMData");
											$CSV_ERROR[] = $str_error;		
											}
										}
										else{
											$str_error = $str_error = $this->_setMessageError($header."-LM");
											$CSV_ERROR[] = $str_error;		
										}
									}
									else{
										$str_error = $str_error = $this->_setMessageError($header."-LM");
										$CSV_ERROR[] = $str_error;	
									}	
									
								}
								elseif (preg_match('/^AN\([0-9]+\)$/', $output[1])) {
									$indexObjAN = $this->_getIndex($output[1],'AN');
									if(isset($output[2])&& $indexObjAN == $z)
									{
										if($CSV['Data'][$index]['QSType']=='QS'){
											$CSV['Data'][$index]['objANX'][$z] = array();
											$CSV['Data'][$index]['objANX'][$z]['ANType'] = $output[2];
											switch ($output[2]) {
												case 'KS':
												case 'KSO':
												case 'KSA':
													$CSV['Data'][$index]['objANX'][$z]['ANData'] = array();
													for ($i = 3; $i < count($output); $i++) {
														if(isset($output[$i])){
															if (preg_match('/^S\([0-9]+\)$/', $output[$i])) {
																$checkInner = false;
																if (isset($output[$i])&&count($CSV['Data'][$index]['objSX'])!=0){
																	foreach ($CSV['Data'][$index]['objSX'] as $key => $value) {
																		if($this -> _getIndex($output[$i], 'S') == $key){
																			$CSV['Data'][$index]['objANX'][$z]['ANData'][] = $this -> _getIndex($output[$i], 'S');
																			$checkInner = true;
																			break;
																		}
																	}
																	if($checkInner==false) {
																		$str_error = $this->_setMessageError($header."-AN(".$z.") 存在しない");
																		$CSV_ERROR[] = $str_error;
																	}
																}
															} else{
																$str_error = $this->_setMessageError($header."-AN(".$z.")");
																$CSV_ERROR[] = $str_error;
																// $CSV['Data'][$index]['objANX'][$z]['ANData'][] = $output[$i];
															}
														}
													}
													if((($output[2]=='KS' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])!=1)||
													($output[2]=='KSA' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])<2)||
													($output[2]=='KSO' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])<2))&&!isset($checkInner)){
														$str_error = $this->_setMessageError($header."-->No_ANX-->".$z);
														$CSV_ERROR[] = $str_error;	
													}
													$z++;
													break;
												default:
													$str_error = $this->_setMessageError($header."-AN(".$z.")");
													$CSV_ERROR[] = $str_error;
													$z++;
													break;
											}
										}
										if($CSV['Data'][$index]['QSType']=='QW'){
											$CSV['Data'][$index]['objANX'][$z] = array();
											$CSV['Data'][$index]['objANX'][$z]['ANType'] = $output[2];
											switch ($output[2]) {
												case 'KWA':
												case 'KWAA':
												case 'KWAO':
												case 'KWP':
												case 'KWPA':
												case 'KWPO':
													$CSV['Data'][$index]['objANX'][$z]['ANData'] = array();
													for ($i = 3; $i < count($output); $i++) {
														if(isset($output[$i])){
															$CSV['Data'][$index]['objANX'][$z]['ANData'][] = $output[$i];
														}
														else{
															$str_error = $this->_setMessageError($header."-AN(".$z.")");
															$CSV_ERROR[] = $str_error;	
														}
													}
													if(($output[2]=='KWA' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])!=1)||
													($output[2]=='KWAA' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])<2)||
													($output[2]=='KWAO' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])<2)||
													($output[2]=='KWP' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])!=1)||
													($output[2]=='KWPA' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])<2)||
													($output[2]=='KWPO' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])<2)){
														$str_error = $this->_setMessageError($header."-AN(".$z.")");
														$CSV_ERROR[] = $str_error;	
													}
													$z++;
													break;
												default:
													$str_error = $this->_setMessageError($header."-AN(".$z.")");
													$CSV_ERROR[] = $str_error;
													$z++;
													break;
											}
										}
										if($CSV['Data'][$index]['QSType']=='FG'){
											$CSV['Data'][$index]['objANX'][$z] = array();
											$CSV['Data'][$index]['objANX'][$z]['ANType'] = $output[2];
											switch ($output[2]) {
												case 'KS':
												case 'KSO':
												case 'KSA':
													$CSV['Data'][$index]['objANX'][$z]['ANData'] = array();
													for ($i = 3; $i < count($output); $i++) {
														if(isset($output[$i])){
															if (preg_match('/^S\([0-9]+\)$/', $output[$i])) {
																$checkInner = false;
																if (isset($output[$i])&&count($CSV['Data'][$index]['objSX'])!=0){
																	foreach ($CSV['Data'][$index]['objSX'] as $key => $value) {
																		if($this -> _getIndex($output[$i], 'S') == $key){
																			$CSV['Data'][$index]['objANX'][$z]['ANData'][] = $this -> _getIndex($output[$i], 'S');
																			$checkInner = true;
																			break;
																		}
																	}
																	if($checkInner==false) {
																		$str_error = $this->_setMessageError($header."-AN(".$z.") 存在しない");
																		$CSV_ERROR[] = $str_error;
																	}
																}
															} else{
																$str_error = $this->_setMessageError($header."-AN(".$z.")");
																$CSV_ERROR[] = $str_error;
																// $CSV['Data'][$index]['objANX'][$z]['ANData'][] = $output[$i];
															}
														}
													}
													if(($output[2]=='KS' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])!=1)||
													($output[2]=='KSA' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])<2)||
													($output[2]=='KSO' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])<2)){
														$str_error = $this->_setMessageError($header."-AN(".$z.")");
														$CSV_ERROR[] = $str_error;	
													}
													$z++;
													break;												
												case 'KWA':
												case 'KWAA':
												case 'KWAO':
												case 'KWP':
												case 'KWPA':
												case 'KWPO':
													$CSV['Data'][$index]['objANX'][$z]['ANData'] = array();
													for ($i = 3; $i < count($output); $i++) {
														if(isset($output[$i])){
															$CSV['Data'][$index]['objANX'][$z]['ANData'][] = $output[$i];
														}
														else{
															$str_error = $this->_setMessageError($header."-AN(".$z.")");
															$CSV_ERROR[] = $str_error;	
														}
													}
													if(($output[2]=='KWA' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])!=1)||
													($output[2]=='KWAA' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])<2)||
													($output[2]=='KWAO' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])<2)||
													($output[2]=='KWP' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])!=1)||
													($output[2]=='KWPA' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])<2)||
													($output[2]=='KWPO' && count($CSV['Data'][$index]['objANX'][$z]['ANData'])<2)){
														$str_error = $this->_setMessageError($header."-AN(".$z.")");
														$CSV_ERROR[] = $str_error;	
													}
													$z++;
													break;
												default:
													$str_error = $this->_setMessageError($header."-AN(".$z.")");
													$CSV_ERROR[] = $str_error;
													$z++;
													break;
											}											
										}										
									}
									else{
										$str_error = $this->_setMessageError($header."-AN(".$z.")");
										$CSV_ERROR[] = $str_error;
										$z++;
									}
								}
								elseif (strcmp($output[1], 'ANC') == 0) {
									$CSV['Data'][$index]['ANC'] = array();
									for ($j = 2; $j < count($output); $j++) {
										if (isset($output[$j])){
											// echo "$header : ANC $output[$j]<br/>";
											if (preg_match('/^AN\([0-9]+\)$/', $output[$j])) {
												// $CSV['Data'][$index]['ANC'][] = $this -> _getIndex($output[$j], 'AN');
												$checkInner = false;
												if (isset($output[$j])&&count($CSV['Data'][$index]['objANX'])!=0){
													foreach ($CSV['Data'][$index]['objANX'] as $key => $value) {
														if($this -> _getIndex($output[$j], 'AN') == $key){
															$CSV['Data'][$index]['ANC'][] = $this -> _getIndex($output[$j], 'AN');
															$checkInner = true;
															break;
														}
														}
													if($checkInner==false) {
														$str_error = $this->_setMessageError($header."-ANC invalid");
														$CSV_ERROR[] = $str_error;
													}
												}
											}
											else{
												$str_error = $this->_setMessageError($header."-ANC");
												$CSV_ERROR[] = $str_error;		
											}
										}
									}
									if(count($CSV['Data'][$index]['ANC'])==0){
										$str_error = $this->_setMessageError($header."-ANC");
										$CSV_ERROR[] = $str_error;	
									}
								}
								elseif (strcmp($output[1], 'INS') == 0) {
									if(isset($output[2])){
										if(strcmp($output[2],'CHK') ==0 ||strcmp($output[2],'CINP') ==0){
											$CSV['Data'][$index]['INS'] = $output[2];
										}
										else{
											$str_error = $this->_setMessageError($header."-INS");
											$CSV_ERROR[] = $str_error;											
										}
									}
									else{
										$str_error = $this->_setMessageError($header."-INS");
										$CSV_ERROR[] = $str_error;
									}
								}
								elseif (preg_match('/^SC\([0-9]+\)$/', $output[1])){// || strcmp($output[1], "SC") == 0) {
									// echo "$output[1]";
									$indexObjSC = $this->_getIndex($output[1],'SC');
									if(isset($output[2]) && $indexObjSC == $t){
										if (preg_match('/^AN\([0-9]+\)$/', $output[2])) {
											$checkInner = false;
											if(isset($output[3])&& ctype_digit(strval($output[3])) && count($CSV['Data'][$index]['objANX'])!=0){
												foreach ($CSV['Data'][$index]['objANX'] as $key => $value) {
													// echo "$header Hungnd $t $key=>".$this -> _getIndex($output[2], 'AN')."<br/>";
													if($this -> _getIndex($output[2], 'AN') == $key){
														$CSV['Data'][$index]['objSC'][$t] = array();
														$CSV['Data'][$index]['objSC'][$t]['AN'] =  $this -> _getIndex($output[2], 'AN');
														$CSV['Data'][$index]['objSC'][$t]['SC'] = $output[3];
														$checkInner = true;
														break;
													}
												}
												if($checkInner==false) {
													$str_error = $this->_setMessageError($header."-SC-AN(".$t.") 存在しない");
													$CSV_ERROR[] = $str_error;
												}
											}
											else{
												$str_error = $this->_setMessageError($header."-SC-AN(".$t.")");
												$CSV_ERROR[] = $str_error;	
											}
										} elseif(strcmp($output[2],'VINP')==0) {
											$CSV['Data'][$index]['objSC'][$t]['VINP'] = $output[2];
										}
										else{
											$str_error = $str_error = $this->_setMessageError($header."-SCデータ-".$output[2]);
											$CSV_ERROR[] = $str_error;
										}
									}
									else{
										$str_error = $str_error = $this->_setMessageError($header."-SC(".$t.")");
										$CSV_ERROR[] = $str_error;
									}
									$t++;
								}
								else{
								$str_error = $str_error = $this->_setMessageError($header);
								$CSV_ERROR[] = $str_error;	
							}	
							}
							else{
								$str_error = $str_error = $this->_setMessageError($header);
								$CSV_ERROR[] = $str_error;	
							}
						} 
						else { //Danh dau la sang cau moi
							// $CSV['Data'][$index] = array();
							if($index == ($q_number+1)){
								// echo "$index > $q_number <br/>";
								if(isset($output[1])&&isset($output[2])){
									if(!isset($CSV['Data'][$index]['ErrorQuestion'])){
									switch($output[1]){
										case "QS":
										case "QW":
										case "FG":
											// echo "$index > $q_number <br/>";
											$CSV['Data'][$index] = array();
											$q_number = $index;
											$x = $y = $z  = $t = 1;
											$CSV['Data'][$index]['QSType'] = $output[1];
											$CSV['Data'][$index]['QSData'] = $output[2];
											$CSV['Data'][$index]['objSX'] = array();
											$CSV['Data'][$index]['objWR'] = array();
											$CSV['Data'][$index]['objANX'] = array();
											$CSV['Data'][$index]['objSC'] = array();
											break;
										default:
											$CSV['Data'][$index] = array();
											$CSV['Data'][$index]['ErrorQuestion'] = true;
											$q_number = $index;
											$str_error = $str_error = $this->_setMessageError($header);
											$CSV_ERROR[] = $str_error;
									}
									}
								}
								else{
									$CSV['Data'][$index] = array();
									$CSV['Data'][$index]['ErrorQuestion'] = true;
									$q_number = $index;
									$str_error = $str_error = $this->_setMessageError($header);
									$CSV_ERROR[] = $str_error;	
								}
							}
							else if($index!=$q_number&& !isset($CSV['Data'][$index]['ErrorQuestion'])){
								$str_error = $str_error = $this->_setMessageError("単位の".$header."がちがい");
								$CSV_ERROR[] = $str_error;
							}
						}
					}
					elseif (strcmp($header, 'Estimate') == 0) {
						// echo $header;
						if(isset($output[1])&&($output[1]=='R'||$output[1]=='P')){
							$CSV['Estimate'] = $output[1];
						}
						else{
							$str_error = $str_error = $this->_setMessageError($header);
							$CSV_ERROR[] = $str_error;								
						}
					}
					elseif (strcmp($header, 'Average') == 0) {
						// echo $header;
					}
					elseif (strcmp($header, 'Ranking') == 0) {
						// echo $header;
					}
					elseif (strcmp($header, 'Trend') == 0) {
						// echo $header;
					}
					elseif (strcmp($header, 'Graphical') == 0) {
						// echo $header;
					}
					elseif (strcmp($header, 'Histgram') == 0) {
						// echo $header;
					}
					else{
						$str_error = $str_error = $this->_setMessageError($header);
						$CSV_ERROR[] = $str_error;		
					}
				}
			}
			if(!isset($CSV['TestTitle'])){
				$str_error = $this->_setMessageError('テストタイプがない');
				$CSV_ERROR[] = $str_error;	
			}
			if(!isset($CSV['TestSubTitle'])){
				$str_error = $this->_setMessageError('サブテストタイプがない');
				$CSV_ERROR[] = $str_error;	
			}
			if(!isset($CSV['StartDateTime'])){
				$str_error = $this->_setMessageError('始まる時間がない');
				$CSV_ERROR[] = $str_error;	
			}
			elseif(isset($CSV['StartDateTime']['MM'])&&
			isset($CSV['StartDateTime']['DD'])&&
			isset($CSV['StartDateTime']['YYYY'])&&
			isset($CSV['StartDateTime']['h'])&&
			isset($CSV['StartDateTime']['m'])&&
			isset($CSV['StartDateTime']['s'])){
			if(!checkdate($CSV['StartDateTime']['MM'],$CSV['StartDateTime']['DD'],$CSV['StartDateTime']['YYYY'])){
				$str_error = $this->_setMessageError('始まる時間の日は存在しない');
				$CSV_ERROR[] = $str_error;	
			}
			else{
				$str1 = $CSV['StartDateTime']['YYYY'].'-'.$CSV['StartDateTime']['MM'].'-'.$CSV['StartDateTime']['DD'].' '.
				$CSV['StartDateTime']['h'].':'.$CSV['StartDateTime']['m'].':'.$CSV['StartDateTime']['s'];
				$start_date_time = new DateTime($str1);
				$now = new DateTime("now");
				if($start_date_time < $now){
					$str_error = $this->_setMessageError('始まる時間は今の時間から設定しなければならない');
					$CSV_ERROR[] = $str_error;		
				}
			}
			}			
			if(!isset($CSV['EndDateTime'])&&!isset($CSV['TestTime'])){
				$str_error = $this->_setMessageError('終わる時間がない');
				$CSV_ERROR[] = $str_error;	
			}
			elseif(isset($CSV['EndDateTime'])&&
			isset($CSV['EndDateTime']['MM'])&&
			isset($CSV['EndDateTime']['DD'])&&
			isset($CSV['EndDateTime']['YYYY'])&&
			isset($CSV['EndDateTime']['h'])&&
			isset($CSV['EndDateTime']['m'])&&
			isset($CSV['EndDateTime']['s'])){
			if(!checkdate($CSV['EndDateTime']['MM'],$CSV['EndDateTime']['DD'],$CSV['EndDateTime']['YYYY'])){
				$str_error = $this->_setMessageError('Not exits_EndDateTime');
				$CSV_ERROR[] = $str_error;	
			}
			elseif(isset($start_date_time)){
				$str2 = $CSV['EndDateTime']['YYYY'].'-'.$CSV['EndDateTime']['MM'].'-'.$CSV['EndDateTime']['DD'].' '.
				$CSV['EndDateTime']['h'].':'.$CSV['EndDateTime']['m'].':'.$CSV['EndDateTime']['s'];
				$end_date_time = new DateTime($str2);
				// debug($start_date_time>$end_date_time);
				if($start_date_time > $end_date_time){
					$str_error = $this->_setMessageError('終わる時間は始まる時間から設定しなければならない');
					$CSV_ERROR[] = $str_error;		
				}				
			}
			}
			if(!isset($CSV['TestType'])){
				$str_error = $this->_setMessageError('テストタイプがない');
				$CSV_ERROR[] = $str_error;	
			}
			if($testee_number==0){
				$str_error = $this->_setMessageError('回答者がいない');
				$CSV_ERROR[] = $str_error;	
			}
			if($q_number==0){
				$str_error = $this->_setMessageError('試験問題がない');
				$CSV_ERROR[] = $str_error;	
			}
			for($q_number=1;$q_number<=count($CSV['Data' ]);$q_number++){
				if(isset($CSV['Data'][$q_number]) && !isset($CSV['Data'][$q_number]['ErrorQuestion'])){
					if (($CSV['Data'][$q_number]['QSType']=='QS' || $CSV['Data'][$q_number]['QSType']=='FG') &&
					count($CSV['Data'][$q_number]['objSX'])==0) {
					$str_error = $this->_setMessageError('SCがない- Q('.$q_number.')');
					$CSV_ERROR[] = $str_error;
					}
					if ($CSV['Data'][$q_number]['QSType']=='QW' && count($CSV['Data'][$q_number]['objWR'])==0) {
					$str_error = $this->_setMessageError('WRがない- Q('.$q_number.')');
					$CSV_ERROR[] = $str_error;
					}
					if (($CSV['Data'][$q_number]['QSType']=='QS' || $CSV['Data'][$q_number]['QSType']=='FG') &&
					count($CSV['Data'][$q_number]['objANX'])==0) {
					$str_error = $this->_setMessageError('回答がない- Q('.$q_number.')');
					$CSV_ERROR[] = $str_error;
					}
					if (count($CSV['Data'][$q_number]['objSC'])==0) {
					$str_error = $this->_setMessageError('SCがない-Q('.$q_number.')');
					$CSV_ERROR[] = $str_error;
					}
					else if(count($CSV['Data'][$q_number]['objSC'])>1){
						for($i=1;$i<=count($CSV['Data'][$q_number]['objSC']);$i++){
							if(isset($CSV['Data'][$q_number]['objSC'][$i]['AN'])){
							for($j=$i+1;$j<=count($CSV['Data'][$q_number]['objSC']);$j++){
								if(isset($CSV['Data'][$q_number]['objSC'][$j]['AN']) && ($CSV['Data'][$q_number]['objSC'][$i]['AN']==$CSV['Data'][$q_number]['objSC'][$j]['AN'])){
								$str_error = $this->_setMessageError("Q($q_number)-SC-AN(".$i.")==AN($j)");
								$CSV_ERROR[] = $str_error;
								break;
								}
							}
							}
						}
					}
					for($i=1;$i<=count($CSV['Data'][$q_number]['objANX']);$i++){
						if(isset($CSV['Data'][$q_number]['objANX'][$i]['ANData'])&&count($CSV['Data'][$q_number]['objANX'][$i]['ANData'])!=0){
						for($j=$i+1;$j<=count($CSV['Data'][$q_number]['objANX']);$j++){
							if($CSV['Data'][$q_number]['objANX'][$i]==$CSV['Data'][$q_number]['objANX'][$j]){
							$str_error = $this->_setMessageError("Q($q_number)-AN(".$i.")==AN($j)");
							$CSV_ERROR[] = $str_error;
							break;
							}
						}
						}
					}
					}
					}
			$return_array['check']='1';
			$return_array['CSV_ERROR'] = $CSV_ERROR;
			$return_array['CSV'] = $CSV;
		}else {
			$return_array['check'] = '0';
		}
		return $return_array;
	}
	
	public function _readjson($examiner_id , $id_link){
		$doc = file_get_contents(VENDORS.$examiner_id."_".$id_link.'.json');
		$CSV = json_decode($doc,true);
		return $CSV;
	}
	public function _readjsonMinh($id_link){
		$doc = file_get_contents(VENDORS.$id_link.'.json');
		$CSV = json_decode($doc,true);
		return $CSV;
	}
	
	//ãƒ­ã‚°ãƒ•ã‚¡ã‚¤ãƒ«ã�«æ›¸ã��
	public function _write($log) {
		//ç�¾åœ¨ã�®æ—¥ä»˜ã‚’èª­ã�¿è¾¼ã�¿
		$current_date = date("d,m,Y");
		$current_date = explode(",", $current_date);
		
		//ãƒ•ã‚¡ã‚¤ãƒ«å­˜åœ¨ã‚’ãƒ�ã‚§ãƒƒã‚¯
		//å¹´ãƒ•ã‚©ãƒ«ãƒ€å­˜åœ¨ã‚’ãƒ�ã‚§ãƒƒã‚¯
		if(!is_dir(VENDORS.$current_date[2])) {
			mkdir(VENDORS.$current_date[2]);
		}
		//æœˆãƒ•ã‚©ãƒ«ãƒ€å­˜åœ¨ã‚’ãƒ�ã‚§ãƒƒã‚¯
		if(!is_dir(VENDORS.$current_date[2]."/".$current_date[1])) {
			mkdir(VENDORS.$current_date[2]."/".$current_date[1]);
		}
		//ãƒ­ã‚°ãƒ•ã‚¡ã‚¤ãƒ«å­˜åœ¨ã‚’ãƒ�ã‚§ãƒƒã‚¯
		if(file_exists(VENDORS.$current_date[2]."/".$current_date[1]."/".$current_date[0]."-".$current_date[1]."-".$current_date[2].".txt")) {
			//ãƒ­ã‚°ç•ªå�·ã‚’èª­ã�¿è¾¼ã�¿
			$txt_file = file(VENDORS.$current_date[2]."/".$current_date[1]."/".$current_date[0]."-".$current_date[1]."-".$current_date[2].".txt");
			$last_line = $txt_file[count($txt_file)-1]; 
			$last_line = explode(",", $last_line);
			$last_line[0] = str_replace('"', '', $last_line[0]);
			$number = (int) $last_line[0] + 1;
			
			//ãƒ­ã‚°æƒ…å ±ã‚’æ›¸ã��
			$file = new File(VENDORS.$current_date[2]."/".$current_date[1]."/".$current_date[0]."-".$current_date[1]."-".$current_date[2].".txt", false, 0644);
			$log = '"'.$number.'", '.$log;
	    	$file->append($log."\n");
			$file->close();	
		} else {
			//ãƒ•ã‚¡ã‚¤ãƒ«ã‚’ä½œæˆ�ã�™ã‚‹
			$file = new File(VENDORS.$current_date[2]."/".$current_date[1]."/".$current_date[0]."-".$current_date[1]."-".$current_date[2].".txt", true, 0644);
			$log = '"1", '.$log;
			$file->append($log."\n");
			$file->close();
		}
	}
}
?>
